using System;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;

namespace Lookstone.IO
{
	/// <summary>
	/// Summary description for Directory.
	/// </summary>
	public static class Directory
	{
		static public string GetAssemblyName()
		{
			Assembly assembly = Assembly.GetEntryAssembly();

			if (assembly == null)
				return string.Empty;

			return assembly.GetName().Name;
		}

		static public string GetAssemblyPath()
		{
			Assembly assembly = Assembly.GetEntryAssembly();

			if (assembly == null)
				return string.Empty;

			return Path.GetDirectoryName(assembly.Location);
		}

		static public string GetAppDataPath()
		{
			Assembly assembly = Assembly.GetEntryAssembly();
			AssemblyCompanyAttribute companyAttr = AssemblyCompanyAttribute.GetCustomAttribute(assembly, typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;
			AssemblyTitleAttribute titleAttr = AssemblyCompanyAttribute.GetCustomAttribute(assembly, typeof(AssemblyTitleAttribute)) as AssemblyTitleAttribute;
			string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), companyAttr.Company, titleAttr.Title);

			VerifyCreatePath(path);
			return path;
		}

		static public string GetPersonalPath()
		{
			Assembly assembly = Assembly.GetEntryAssembly();
			AssemblyCompanyAttribute companyAttr = AssemblyCompanyAttribute.GetCustomAttribute(assembly, typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;
			AssemblyTitleAttribute titleAttr = AssemblyCompanyAttribute.GetCustomAttribute(assembly, typeof(AssemblyTitleAttribute)) as AssemblyTitleAttribute;
			string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), companyAttr.Company, titleAttr.Title);

			VerifyCreatePath(path);
			return path;
		}

		/// <summary>
		/// Verifies that a path exists, and if not, creates it
		/// </summary>
		/// <param name="path">Path to verify and / or create</param>
		/// <returns>True if the path exists or was created; false if the 
		/// path was not able to be created</returns>
		public static bool VerifyCreatePath(string path)
		{
			try
			{
				// Verify that the target path exists, and if not, attempt to 
				// create it.
				if (!System.IO.Directory.Exists(path))
				{
					DirectoryInfo dirInfo = System.IO.Directory.CreateDirectory(path);

					if (!dirInfo.Exists)
						return false;
				}

				return true;
			}
			catch
			{
				return false;
			}
		}
	}
}
